// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

/*!
\file
\brief Implementation of Environmental/Analysis Options 
*/

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "options.h"
#include "pmaths.h"
#include "suffixes.h"
#include "utils.h"

int options_init(struct selected_options *ops) {
	if (ops == NULL) return 0;

	ops->isMap3File = FALSE;
	ops->isGvar = FALSE;
	ops->loadGvar = FALSE;
	*ops->szPheFileName = *ops->szPedFileName = *ops->szMapFileName = '\0';
	*ops->szSampleFileName = *ops->szPermFileName = '\0';
	*ops->szIpedFileName = *ops->szBmapFileName = '\0';
	*ops->szExcludeListFileName = *ops->szBpedFileName = *ops->szSpedFileName = '\0';
	*ops->szSeedFileName = *ops->szMissingnessFileName = *ops->szFoundersFileName = '\0';
	*ops->szSolFileName = '\0';
	*ops->rdir = '\0';
	ops->isCmMap = FALSE;
	ops->isSnpMajor = TRUE;

	ops->mpheno = DEFAULT_MPHENO; 

	// PED File options.
	ops->pedSkipFid = FALSE;
	ops->pedSkipParents = FALSE;
	ops->pedSkipSex = FALSE;
	ops->pedSkipPhenotype = FALSE;

	// Initialise missing value options.
	strcpy(ops->missing_genotype, "0");
	strcpy(ops->missing_phenotype, "-9");
	ops->preserve_all_genotypes = FALSE;
	ops->coding01 = FALSE;
	ops->liability = FALSE;
	ops->ignore_missing_sex = TRUE;
	ops->qt = FALSE; 
	ops->bt = TRUE;  // Binary Trait (0, 1, 2)

	// Directory for output files.
	strcpy(ops->ldir, ".\\");
	sprintf(ops->szRunId, "run_%i", time(NULL)); // Initialise RUN ID to current system clock.

	// SNP filtering options
	ops->has_nonfounders = FALSE;
	ops->oblig_missing = FALSE;
	ops->summ_nonfounders = FALSE;
	ops->af_count = FALSE;

	// Initialise numerical constants
	ops->MAX_IND_MISSING = DEFAULT_MIND;
	ops->MAX_GENO_MISSING = DEFAULT_GENO;
	ops->epsilon = 0.0;
	ops->min_af = DEFAULT_MAF;
	ops->max_af = 1.0;
	ops->min_geno_cell = 5;
	ops->pfvalue = 0.00001;

	// Initialise permutation constants
	ops->nperm = DEFAULT_NPERM;
	ops->model_perm_allelic = ops->model_perm_gen = ops->model_perm_dom = 
		ops->model_perm_rec = ops->model_perm_trend = FALSE;

	ops->verbose = TRUE;

	// EPI values
	ops->epi_alpha1 = DEFAULT_EPI_ALPHA1; // Threshold to display.
	ops->epi_alpha2 = DEFAULT_EPI_ALPHA2;	  // Threshold for count.
	ops->epi_alpha = fabs(ltqnorm(0.95 / 2));
	ops->xchr_model = 1;      // Default 'X' chromosome coding scheme.
	ops->sex_effect = FALSE;

	// Categorical filter setting, all off by default.
	ops->filter_males = ops->filter_females = 
		ops->filter_cases = ops->filter_controls = FALSE;
	ops->no_filtering = FALSE;

	// The HWE variables
	ops->HWD_test = ops->HWD_filter_on_all = FALSE;
	ops->HWD_standard = FALSE;
	ops->HWD_limit = DEFAULT_HWE_LIMIT;

	ops->logRemovals = FALSE;

	// CMH Cluster settings.
	ops->nk = 0; // By default no clusters are specified.
	ops->ci_level = DEFAULT_CI_LEVEL;
	ops->CMH_test_1 = ops->CMH_test_2 = FALSE;
	ops->breslowday = TRUE; // Do BreslowDay by default.

	// PValue adjustment settings.
	ops->use_GC = ops->fix_lambda = FALSE;
	*ops->szAdjustedFileName = '\0';

	// Default sort column.
	strcpy(ops->szSortColumnName, DEFAULT_SORT_COL_NAME);

	// Named phenotype/stratification columns.
	*ops->szPheColumnName = *ops->szSolColumnName = '\0';

	// LD sampling parameters.
	ops->disp_r_window_snp = DEFAULT_DISP_SNP_WINDOW;
	ops->disp_r_window_kb = DEFAULT_DISP_SNP_WINDOW_KB;

	return 1;
}
